import {v4} from 'uuid'
import {split, ApolloLink} from 'apollo-link';
import {setContext} from 'apollo-link-context';
import {createHttpLink} from 'apollo-link-http';
import {ApolloClient} from 'apollo-client'
import {InMemoryCache, NormalizedCacheObject} from 'apollo-cache-inmemory'
import {getMainDefinition} from 'apollo-utilities';
import gql from 'graphql-tag'

export * from 'ramda'

//千位符
export const showAmt = (amt: number) => {
    let str = (amt || 0).toString().replace(/\d{1,3}(?=(\d{3})+$)/g, '$&,')
    if (str.indexOf('.') < 0) {
        str += '.00'
    }
    return str
}

const getNumStr = (num: number) => num > 9 ? num : '0' + num

export const getDate = (time: string | object | unfefined) => {
    if (!time) return '--' //更改此行返回值时，同时更改Grid.js文件中的value(title)!= '--' ? :
    const date = new Date(time)
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    return year + '-' + getNumStr(month) + '-' + getNumStr(day);
}

export const getDateTime = (time: string | object | unfefined) => {
    if (!time) return '--'
    const date = new Date(time)
    const hour = date.getHours();
    const minute = date.getMinutes();

    return getDate(time) + ' ' + getNumStr(hour) + ':' + getNumStr(minute)
}


export const getTitle = (identity) => {
    switch (identity) {
        case 'personal':
            return '个人';
        case 'enterprise':
            return '企业';
        case 'third':
            return '第三方';
    }
};

//getOptionDispalyValue
export const getOptionDisplayValue = (value, options) => {
    if (!value) return '-'
    const option = (options || []).find(x => x.value == value)
    if (!option) return '-'
    return option.displayValue
};

//回车执行
export const enterEntered = handler => e => {
    if (e.key === 'Enter') handler()
};

export const omitStr = (str, begin, end) => {
    if (str) {
        if (str.length > end) {
            return (str.substring(begin, end) + '...')
        } else {
            return str
        }
    }
}

//计算工作经验
export const calcWorkTimes = (beginWorkTime) => {
    if (!beginWorkTime) return '未知工作经验';
    const times = Math.floor((new Date() - new Date(beginWorkTime)) / 1000 / 60 / 60 / 24 / 360);
    return times < 1 ? '一年以内' : times + '年工作经验'
}

// /////////imagesSaver Function   start ////////////////
export const initFileToUpload: Fn1<File, FileObj> = file => ({
    file,
    name: file.name,
    key: `${v4()}.${file.name.split('.')[1]}`,
    size: file.size,
    uploadingProgress: 0
})
